const WebSocket = require('ws');
var quertString = require('querystring');
class ws {
  static online = 0 // 在线连接
  static ws = WebSocket.Server //默认实例
  static init(server) {
    // 创建实例
    this.ws = new WebSocket.Server({ server, path: '/ws' });
    this.ws.on('connection', async (ws, request) => {
      if (!(request.url.includes('/ws'))) {
        return ws.close();
      }
      this.online = this.ws._server._connections;
      // console.log(this.ws.clients);
      console.log(`socket当前在线${this.online}个连接`)
      let index = request.url.indexOf('?');
      let search = request.url.slice(index + 1);
      const params = quertString.parse(search);
      if (!params.id) {
        return ws.close();
      }
      try {
        //do something
        // 这里可以做一些加强判断查询数据库等行为
        ws.id = params.id // 添加ws实例的唯一标识
        const obj = { "message": "连接成功", "retCode": 200,  id: params.id }
        ws.send(JSON.stringify(obj))
      } catch (error) {
        console.log('websocket connection error', error)
        return ws.close();
      }
    });
  }
  // 发送客户端数据
  // 发送客户端数据
  static sendToAllCliect(Data) {
    let iskeep = false // 加个变量做下发成功判断
    if (!(this.ws instanceof WebSocket.Server)) {
      return iskeep;
    }
    const { id } = Data
    this.ws.clients.forEach((client, index) => {
      client.send(JSON.stringify(Data)); 
    });
    return iskeep;
  }
  // 发送客户端数据
  static sendToCliect(Data) {
    let iskeep = false // 加个变量做下发成功判断
    if (!(this.ws instanceof WebSocket.Server)) {
      return iskeep;
    }
    const { id } = Data
    this.ws.clients.forEach((client, index) => { 
      if (client.readyState === WebSocket.OPEN && client.id === id) {
        // 发送给指定匹配id
        client.send(JSON.stringify(Data));
        iskeep = true
      }
    });
    return iskeep;
  }
}
module.exports = ws